import logging

import pytest
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options

from VAR.VAR import TH_ADMIN_URL
from key_word.key_word import KeyWord
from locator.login import *


@pytest.fixture()
def browser():
    # 添加无头模式-浏览器后台运行
    options = Options()
    # options.add_argument("--headless")
    driver = webdriver.Chrome(options=options)
    # driver = webdriver.Chrome(options=options,executable_path='/usr/bin/chromedriver')

    driver.maximize_window()

    # 返回驱动driver
    yield driver

    # 后置
    driver.quit()


def login_suc(browser):
    browser.get(TH_ADMIN_URL)
    browser.find_element(*page_login_username).send_keys('admin')
    browser.find_element(*page_login_password).send_keys('a123456789')
    ActionChains(browser).drag_and_drop_by_offset((browser.find_element(*page_login_block)), 400, 40).perform()
    browser.find_element(*page_login_btn).click()


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    out = yield
    res = out.get_result()
    if res.when == 'call':
        logging.info(f"测试编号{res.nodeid}")
        logging.info(f"测试步骤{res.outcome}")
        logging.info(f"故障详情{res.longrepr}")
        logging.info(f"异常{call.excinfo}")
        logging.info(f"用例耗时{res.duration}")

